const pool=require('../../Model/query')
const formidable = require('formidable');
const fs = require('fs');
const path = require('path');

const upload=(req, res)=>{
  if (!req.file) {
    return res.send({
      code:400,
      msg:'请上传图片'
    })
  }
  const  sql=`INSERT INTO uploads (id,filename,filepath,size) VALUES(null,?,?,?)`
  pool(sql,[req.file.filename,`/uploads/${req.file.filename}`,req.file.size]).then(res1=>{
    res.send({
      code:200,
      msg:'图片上传成功',
      data:{
        filepath: `/uploads/${req.file.filename}`
      }
    });
  }).catch(()=>{
    res.send({
      code:500,
      msg:'图片上传失败'
    });
  })
}

module.exports={
  upload
}